cc.Class({
    extends: cc.Component,
    properties: {
        account:null,
	    userId:null,
		userName:null,
		lv:0,
		exp:0,
		coins:0,
		gems:0,
		sign:0,
        ip:"",
        roomData:null,
        oldRoomId:null,
        // 俱乐部信息（标识是否俱乐部房间）
        isCreate:false,
        // 房卡变化
        gemsChange:false
    },
    
    guestAuth:function(){
        var account = cc.args["account"];
        if(account == null){
            account = cc.sys.localStorage.getItem("account");
        }
        
        if(account == null){
            account = Date.now();
            cc.sys.localStorage.setItem("account",account);
        }
        
        cc.vv.http.sendRequest("/guest",{account:account},this.onAuth);
    },
    
    onAuth:function(ret){
        var self = cc.vv.userMgr;
        if(ret.errcode !== 0){
            console.log(ret.errmsg);
        }else{
            self.account = ret.account;
            self.sign = ret.sign;
            cc.vv.http.url = "http://" + cc.vv.SI.hall;
            self.login();
        }   
    },
    
    login:function(){
        var self = this;
        var onLogin = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                if(!ret.userid){
                    //jump to register user info.
                    // 微信的没有创建角色，清空缓存重新登录
                    if (cc.sys.os == cc.sys.OS_ANDROID||cc.sys.os == cc.sys.OS_IOS) {
                        // cc.sys.localStorage.removeItem("wx_account");
                        // cc.sys.localStorage.removeItem("wx_sign");
                        cc.vv.wc.hide()
                    }else{
                        cc.director.loadScene("createrole")
                    }
                }else{
                    console.log("登录返回",ret);
                    self.account = ret.account;
        			self.userId = ret.userid;
        			self.userName = ret.name;
        			self.lv = ret.lv;
        			self.exp = ret.exp;
        			self.coins = ret.coins;
        			self.gems = ret.gems;
                    self.roomData = ret.roomid;
                    self.sex = ret.sex;
                    self.ip = ret.ip;
        			cc.director.loadScene("mjhall");
                    // cc.director.loadScene("mjgame");
                    // 成功登陆后获取俱乐部的sockte地址
                    self.connectClubServer()
                }
            }
        };
        var failFn = function(){
            console.log("failFn");
            // if (cc.vv.wc.node) {
                cc.vv.wc.hide()
            // }
        };
        cc.vv.wc.show("正在登录游戏");
        cc.vv.http.sendRequest("/login",{account:this.account,sign:this.sign},onLogin,null,failFn,this)
    },
    connectClubServer:function(){
        console.log("connectClubServer");
        var onGet = function(ret){
            console.log("/enterClub"+JSON.stringify(ret));
            if(ret.errcode !== 0){
                console.log("/enterClub"+ret.errmsg);
                // this.connectClubServer();
                cc.vv.alert.show("提示","登陆俱乐部失败请重登")
            }else{
                // 进入俱乐部成功，开启socket
                cc.vv.JuleNet.ip = ret.ip + ":" + ret.port;
                console.log("cc.vv.net.ipJule",cc.vv.JuleNet.ip);
                var onConnectJuleOK = function(){
                    console.log("onConnectJuleOK");
                    // 先登录一次
                    cc.vv.JuleNet.send("login_club",{userid:cc.vv.userMgr.userId});
               };
                var onConnectJuleFailed = function(){
                    console.log("onConnectJuleFailed");
                    // cc.vv.alert.show("提示","获取俱乐部消息失败请重进俱乐部")
                };
                cc.vv.JuleNet.connect(onConnectJuleOK,onConnectJuleFailed)
            }
        };
        var data = {};
        cc.vv.http.sendRequest("/enterClub",data,onGet)
    },
    create:function(name){
        var self = this;
        var onCreate = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                self.login()
            }
        };
        
        var data = {
            account:this.account,
            sign:this.sign,
            name:name
        };
        cc.vv.http.sendRequest("/create_user",data,onCreate) 
    },
    
    enterRoom:function(roomId,callback){
        console.log("enterRoom",roomId);
        var self = this;
        var complete = false;
        var onEnter = function(ret){
            console.log("进入房间返回",ret);
            complete = true;
            if(ret.errcode !== 0){
                if(ret.errcode == -1){
                    setTimeout(function(){
                        self.enterRoom(roomId,callback)
                    },5000)
                }else{
                    cc.vv.wc.hide();
                    if(callback != null){
                        callback(ret)
                    }
                }
            }else{
                if(callback != null){
                    callback(ret)
                };
                cc.vv.gameNetMgr.connectGameServer(ret);
                // cc.vv.gameNetMgr.connectRet=ret;
            }
        };
        var gpsArr = [-1,-1];
        if(cc.vv.anysdkMgr.getGPSLocation()){
            var gps = cc.vv.anysdkMgr.getGPSLocation();
            gpsArr = gps.split("_");
            console.log("经纬度",gpsArr)
        };

        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            roomid:roomId,
            lat:gpsArr[0],
            lng:gpsArr[1]
        };
        // 重连没有显示
        // if (!cc.vv.gameNetMgr.isReconnect) {
            cc.vv.wc.show("正在进入房间 " + roomId)
        // };
        
        //加入进入房间重试
        var xhr = null;
        var fnRequest = function(){
            if(!complete){
                if(xhr){
                    xhr.abort()
                };
                console.log("进入房间/enter_private_room",data);
                xhr = cc.vv.http.sendRequest("/enter_private_room",data,onEnter);
                setTimeout(fnRequest,5000)
            }          
        };
        //执行进入房间
        fnRequest()
    },
    getHistoryList:function(callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                console.log(ret.history);
                if(callback != null){
                    callback(ret.history)
                }
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign
        };
        cc.vv.http.sendRequest("/get_history_list",data,onGet)
    },
    getGamesOfRoom:function(uuid,callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                console.log(ret.data);
                callback(ret.data)
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            uuid:uuid
        };
        cc.vv.http.sendRequest("/get_games_of_room",data,onGet)
    },
    
    getDetailOfGame:function(uuid,index,callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg)
            }else{
                console.log("历史记录数据",ret.data);
                callback(ret.data)
            }       
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            uuid:uuid,
            index:index
        };
        cc.vv.http.sendRequest("/get_detail_of_game",data,onGet)
    },
    refreshGemsTip:function(){
        console.log("refreshGemsTip");
        var self=this;
        var sucFn=function(ret){
            console.log("ret",JSON.stringify(ret));
            self.shareErrode=ret.errcode;
            self.gemsChange=true;
            if(ret.errcode != 0){
                console.log(ret.errmsg);
                self.shareErrmsg=ret.errmsg
            }else{
                console.log("分享成功回调");
                self.gems=ret.gems
            }
        };
        cc.vv.http.sendRequest("/share",{userid:cc.vv.userMgr.userId},sucFn)
    }
});
